#include "myTasks.h"
#include <QDebug>
#include <QElapsedTimer>
#include <QThread>

void AxisAdjust::doAdjust(const QVector<qreal> data)
{

    QElapsedTimer timer;
    timer.start();
    if(data.size() != 5){
        qDebug() << "doAdjust() error";
        return;
    }
    QVector<qreal> ampList;
    //这里没考虑三图横坐标范围不同的情况，这里以AX横坐标为准
    int size = numVis[0].size();
    qreal cap = data[4];
    for(int i = 0; i < 3; ++i){
        //插入新值
        numVis[i].push_back(data[i]);
        hashMap[i][data[i]]++;
        if(size > cap){
            hashMap[i][numVis[i].front()]--;
            if(hashMap[i][numVis[i].front()] == 0)
                hashMap[i].remove(numVis[i].front());
            numVis[i].pop_front();
        }
        //如果不需要显示全部，输出当前区域最值
        if(data[5] == 0){
            auto itBeg = hashMap[i].begin().key();
            auto itEnd = (hashMap[i].end()-1).key();
            itBeg = itBeg > 0 ? (0.9*itBeg > 0.01 ? 0.9*itBeg : 0.01) : (1.1*itBeg < -0.01 ? 1.1*itBeg : -0.01);
            itEnd = itEnd > 0 ? (1.1*itEnd > 0.01 ? 1.1*itEnd : 0.01): (0.9*itEnd < -0.01 ? 0.9*itEnd : -0.01);
            ampList.push_back(itBeg);
            ampList.push_back(itEnd);
        }
        //更新全域最值
        if(data[i] < extremList[2*i]) extremList[2*i] = data[i];
        if(data[i] > extremList[2*i+1]) extremList[2*i+1] = data[i];
    }
    emit resultReady( data[5] ? extremList : ampList );
//emit resultReady(QList<qreal>{-1,1,-1,1,-1,1});
qDebug() << "doAdjust() cost " << timer.elapsed() << " ms in " << QThread::currentThread();
}





void DataDisposal::doDisposal(const QByteArray data)
{

    QElapsedTimer timer;
    timer.start();
    short acc_d[3] = {0};
    short angle_d[3] = {0};
    char *p_acc = (char *)&acc_d;
    char *p_ang = (char *)&angle_d;
    for(int i = 0; i < 6; i++)      //小端系统适用,大端系统交换顺序即可
    {                               //以char指针按顺序存储数据（先低八位，后高八位 eg:AB CD），以short型指针读取，读取到为先高后低（eg:CDAB）
        p_acc[i] = data[2+i];

        p_ang[i] = data[14+i];

    }

    for(int i = 0; i <3; i++){
        qreal num = (qreal)acc_d[i]/32768*16;
        AccWithGList[i] = num;

        num = (qreal)angle_d[i]/32768*180;
        AngList[i] = num;
    }

//--------------------------------------------滤除重力加速度--------------------------------------->
    double gama =AngList[0] / 180 * 3.141592654;
    double beta =AngList[1] / 180 * 3.141592654;
    double alpha =AngList[2] / 180 * 3.141592654;
    AccWithoutGList[0]=AccWithGList[0]-1.029*(-sin(beta));
    AccWithoutGList[1]=AccWithGList[1]-1.029*(cos(beta)*sin(gama));
    AccWithoutGList[2]=AccWithGList[2]-1.029*(cos(beta)*cos(gama));
    this->dataList.clear();
    this->dataList.append(AccWithGList);
    this->dataList.append(AccWithoutGList);
    this->dataList.append(AngList);

    emit dataReady(dataList);
qDebug() << "doDisposal() cost " << timer.elapsed() << " ms in " << QThread::currentThread();
}
